package com.web.security.handler;

import com.common.apiresult.CommonResult;
import com.common.apiresult.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liuxuhuang
 * @date 2023年04月02日 10:31
 * @desc 登录认证失败处理器
 */
@Component
public class NoAuthenricationHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("text/json;charset=utf8");
        httpServletResponse.getWriter().println(new ObjectMapper().writeValueAsString(CommonResult.failed(ResultCode.UNAUTHORIZED)));
    }
}
